<?php

/**
 * 
 */
namespace User\Controller;
use Common\Controller\HomeBaseController;
class PublicController extends HomeBaseController {
	
	
	function _initialize() {
		parent::_initialize();
		$this->config_user=F("Config_user");
	
	
	
		if($this->u){
			$this->error("已经登录！",$this->jump_self);
	
		}
	
	
	}



	


	//登录
	public function login(){
		$this->display("");
	}
	
	//登录验证
	function dologin(){
		
		
		
	
		$verify = I("post.verify");
	
				    	if(empty($verify)){
		    		$this->error("验证码不能为空！");
		    	}
		    	if(!check_verify($verify)){
		    		               $this->error('验证码输入错误！');
		    	            }
		 
		$users_model=M("Users");
		$rules = array(
				//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
	
				array('username', 'require', '用户名或者邮箱不能为空！', 1 ),
				array('password','require','密码不能为空！',1),
				 
		);
		if($users_model->validate($rules)->create()===false){
			$this->error($users_model->getError());
		}
		 
		extract($_POST);
		 
		if(strpos($username,"@")>0){//邮箱登陆
			$where['email']=$username;
		}else{
			$where['name']=$username;
		}
		$users_model=M('Users');
		$result = $users_model->where($where)->find();
		 
	
	
		//exit();
		if($result != null)
		{
			//$this->error ($password.$result[pwd_hash]);
			if($result['pass'] == lap_password($password,$result[pwd_hash]))
			{
				 
				if($result[status]==0){
					$this->error("该账户已经禁用！");
	
				}

				if($result[shenhe]==0){
					$this->error("该账户正在审核中！");

				}
		
			
				$this->_do_login_data($result[id]);
				//写入此次登录信息
				$data = array(
						'last_login_time' => date("Y-m-d H:i:s"),
						'last_login_ip' => get_client_ip(),
				);
				$users_model->where("id=".$result["id"])->save($data);
				 
				cookie("jump_self",null);
				
				if($result[email_status]==0){
					$this->success("登录验证成功！",U("User/Active/index"));
				
				}
				else{
					$this->success("登录验证成功！",$this->jump_self);
					
				}
	
	
			
			}else{
				$this->error("密码错误！");
			}
		}else{
			$this->error("用户名不存在！");
		}
	
	}
	
	//注册
	function register(){
	
	
		$this->display("");
		
		
		
		
	}
	
	//注册
	function register_company(){
	
	
		$this->display("");
	
	
	
	
	}
	
	//处理注册请求
	function doregister(){
		$verify = I("post.verify");
	
				    	if(empty($verify)){
		    		$this->error("验证码不能为空！");
		    	}
		    	if(!check_verify($verify)){
		    		               $this->error('验证码输入错误！');
		    	            }
	
		 
		$users_model=M("Users");
		$rules = array(
				//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
				array('agreement', 'require', '您未同意服务条款！', 1 ),
				array('model', 'require', '类型不能为空！', 1 ),
				array('username', 'require', '账号不能为空！', 1 ),
				array('email', 'require', '邮箱不能为空！', 1 ),
				array('password','require','密码不能为空！',1),
				array('repassword', 'require', '重复密码不能为空！', 1 ),
				array('repassword','password','确认密码不正确',0,'confirm'),
				array('email','email','邮箱格式不正确！',1), // 验证email字段格式是否正确
	
		);
		if($users_model->validate($rules)->create()===false){
			$this->error($users_model->getError());
		}
		 
		extract($_POST);
		//用户名需过滤的字符的正则
		$stripChar = '?<*.>\'"';
		if(preg_match('/['.$stripChar.']/is', $username)==1){
			$this->error('用户名中包含'.$stripChar.'等非法字符！');
		}
		 
		$banned_usernames=explode(",",  $this->config_user['BANNED_NAME']);
		 
		if(in_array($username, $banned_usernames)){
			$this->error("此用户名禁止使用！");
		}
		 
		if(strlen($password) < 5 || strlen($password) > 20){
			$this->error("密码长度至少5位，最多20位！");
		}
		 
	
		$where['name']=$username;
		$where['email']=$email;
		$where['_logic'] = 'OR';
	
		$users_model=M("Users");
		$result = $users_model->where($where)->count();
		 
		if($result){
			$this->error("用户名或者该邮箱已经存在！");
		}else{
			$uc_register=true;
	
			if($uc_register){
				$need_email_active=$this->config_user['USER_REGISTER_EMAIL_STATUS'];
				$need_shenhe=$this->config_user['USER_REGISTER_SHENHE'];

				$pwd_hash=rand_string();
	
				$data=array(
						'name' => $username,
						'email' => $email,
						'nickname' =>$username,
						'pass' => lap_password($password,$pwd_hash),
						'last_login_ip' => get_client_ip(),
						'create_time' => time(),
						'last_login_time' =>  time(),
						'status' => 1,
					    'shenhe'=>$need_shenhe?0:1,
						'email_status' => $need_email_active?0:1,
						"model"=>$model,
						"pwd_hash"=>$pwd_hash,
				);
				$rst = $users_model->add($data);
				
				
				if($rst){
					//登入成功页面跳转
				
					 

					 
					//发送激活邮件
					if($need_email_active){
						$this->_send_to_active();
						
						$this->success("注册成功，激活后才能使用！",__ROOT__."/");
					}
					elseif($need_shenhe){
						$this->success("注册成功！请等待管理员验证",$this->jump_self);
					}

					else {


						$this->_do_login_data($rst);
						$this->success("注册成功！",$this->jump_self);

					}
					cookie("jump_self",null);
				}else{
					$this->error("注册失败！",U("User/public/register"));
				}
				 
			}else{
				$this->error("注册失败！",U("User/publick/register"));
			}
			 
		}
	
	
	}
	
	
	


	
	//提交忘记密码页面
	function forgot_password(){
		
		if(IS_POST){
			
			$verify = I("post.verify");
			
					    	if(empty($verify)){
			    		$this->error("验证码不能为空！");
			    	}
			    	if(!check_verify($verify)){
			    		               $this->error('验证码输入错误！');
			    	            }
			
				$users_model=M("Users");
				$rules = array(
						//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
						array('email', 'require', '邮箱不能为空！', 1 ),
						array('email','email','邮箱格式不正确！',1), // 验证email字段格式是否正确
		
				);
				if($users_model->validate($rules)->create()===false){
					$this->error($users_model->getError());
				}else{
					$email=I("post.email");
					$find_user=$users_model->where(array("email"=>$email))->find();
					if($find_user){
						$this->_send_to_resetpass($find_user);
					
						$this->success("密码重置邮件发送成功,请重新登录！",U("User/Public/login"));
					}else {
						$this->error("账号不存在！");
					}
		
				}
		
			
		
		}
		else{
		$this->display("");
		}
	}
	

	//发送密码找回邮件
	protected  function _send_to_resetpass($user){
	
		$config = array_merge(F("Config_site"), F("Config_user"));
		if(!$config){
			$this->error('网站未配置账号激活信息，请联系网站管理员');
		}
		//邮件标题
		$title = $config['USER_FORGET_TITLE'];
		$uid=$user['id'];
		$username=$user['name'];
	
		$activekey=md5($uid.time().uniqid());
		$users_model=M("Users");
	
		$result=$users_model->save(array("id"=>$uid,"user_activation_key"=>$activekey));
		if(!$result){
			$this->error('密码重置激活码生成失败！');
		}
		//生成激活链接
		$url = U('User/Public/password_reset',array("hash"=>$activekey), "", true);
		//邮件内容
		$template = $config['USER_FORGET'];
	
		$content = str_replace(array('http://#link#','#username#'), array($url,$username),$template);
	
		
		$send_result=SendMail($user['email'], $title, $content);
	
		if($send_result['error']){
			$this->error('密码重置邮件发送失败！');
		}
	}
	//找回密码页面
	function password_reset(){
		
		if(IS_POST){
			$verify = I("post.verify");
				
					    	if(empty($verify)){
			    		$this->error("验证码不能为空！");
			    	}
			    	if(!check_verify($verify)){
			    		               $this->error('验证码输入错误！');
			    	            }
		
				$users_model=M("Users");
				$rules = array(
						//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
						array('password', 'require', '密码不能为空！', 1 ),
						array('repassword', 'require', '重复密码不能为空！', 1 ),
						array('repassword','password','确认密码不正确',0,'confirm'),
						array('hash', 'require', '重复密码激活码不能空！', 1 ),
				);
				if($users_model->validate($rules)->create()===false){
					$this->error($users_model->getError());
				}else{
					$hash=I("post.hash");
					$pwd_hash=$users_model->where(array("user_activation_key"=>$hash))->getField("pwd_hash");
					$password=lap_password(I("post.password"),$hash);
					
					$result=$users_model->where(array("user_activation_key"=>$hash))->save(array("pass"=>$password,"user_activation_key"=>""));
					if($result){
					
						$this->success("密码重置成功，请登录！",U("User/Public/login"));
					}else {
						$this->error("密码重置失败，重置码无效！");
					}
			
				}
			
			
			
		}
		else{
			
		$this->display("");
		}
	}











}
?>
